package br.unifor.cct.mia.dataenhancement;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class DatabaseUtil {
		
	private String dataSetData;
	
	public DatabaseUtil(String dataSetData) {
		this.dataSetData = dataSetData;
	}
	
	private BufferedReader createReader() {
		BufferedReader reader = null;
		FileInputStream fIn;
		try {
			fIn = new FileInputStream(dataSetData);
			reader = new BufferedReader(new InputStreamReader(fIn));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}	
		return reader;
	}
	
	
	public String readLine(int lineNumber) {	
		BufferedReader reader = createReader();
		String line = null;
		if ( lineNumber == 0 ) {
			try {
				line = reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else {
			int count = 0;
			while( count != lineNumber ) {
				try {
					line = reader.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				count++;
			}
		}		
		if ( line == null ) {
			//System.out.println("Line == null");
			return null;
		}
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		line = line.replaceAll(" ",",");
		line += "\n";
		return line;
	}
	
	/**
	 * @deprecated
	 */
	public static String readLine(String dataSetData, int lineNumber) {
		String line = null;
		
		try {
			FileInputStream fIn = new FileInputStream(dataSetData);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fIn));
			
			if ( lineNumber == 0 ) {
				line = reader.readLine();
			}
			else {
				int count = 0;
				while( count != lineNumber ) {
					line = reader.readLine();
					count++;
				}
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if ( line == null ) return null;
		
		line = line.replaceAll(" ",",");
		line += "\n";
		
		return line;
	}

}
